export default  {
	// 1.黑白调
	a: function(imageData) {
		for (let i = 0; i < imageData.data.length; i += 4) {
			let r = imageData.data[i];
			let g = imageData.data[i + 1];
			let b = imageData.data[i + 2];
			if (r > 255 / 2) {
				imageData.data[i] = 255
				imageData.data[i + 1] = 255
				imageData.data[i + 2] = 255
			} else if (r < 255 / 2) {
				imageData.data[i] = 0
				imageData.data[i + 1] = 0
				imageData.data[i + 2] = 0
			}
		}
		return imageData
	},
	// 2.灰色调
	b: function(imageData) {
		for (let i = 0; i < imageData.data.length; i += 4) {
			let r = imageData.data[i];
			let g = imageData.data[i + 1];
			let b = imageData.data[i + 2];
			let average = Math.floor((r + g + b) / 3);
			imageData.data[i] = average;
			imageData.data[i + 1] = average;
			imageData.data[i + 2] = average;
		}
		return imageData
	},
	// 3.反转
	c: function(imageData) {

		for (let i = 0, len = imageData.data.length; i < len; i += 4) {
			imageData.data[i] = 255 - imageData.data[i];
			imageData.data[i + 1] = 255 - imageData.data[i + 1]
			imageData.data[i + 2] = 255 - imageData.data[i + 2];
		}
		return imageData
	},
	// 4.复古
	d: function(imageData) {
		for (let i = 0; i < imageData.data.length; i += 4) {
			let r = imageData.data[i];
			let g = imageData.data[i + 1];
			let b = imageData.data[i + 2];
			imageData.data[i] = (r * 0.393) + (g * 0.769) + (b * 0.189);
		}
		return imageData
	},
	// 5.红色蒙版
	e: function(imageData) {

		for (let i = 0; i < imageData.data.length; i += 4) {
			let r = imageData.data[i];
			let g = imageData.data[i + 1];
			let b = imageData.data[i + 2];
			imageData.data[i] = (r + g + b) / 3;
			imageData.data[i + 1] = 0;
			imageData.data[i + 2] = 0;
		}
		return imageData
	},
	// 6.增加亮度
	f: function(imageData, delta=100) {

		for (let i = 0; i < imageData.data.length; i += 4) {
			imageData.data[i] = imageData.data[i]+delta;
			imageData.data[i + 1] = imageData.data[i+1]+delta;
			imageData.data[i + 2] = imageData.data[i+2]+delta;
		}
		return imageData
	},
	// 7.浮雕
	g: function(imageData) {
		let w = imageData.width,
			h = imageData.height;

		for (let i = h; i > 0; i--) { // 行
			for (let j = w; j > 0; j--) { // 列
				for (let k = 0; k < 3; k++) {
					let num = (i * w + j) * 4 + k;
					let numUp = ((i - 1) * w + j) * 4 + k;
					let numDown = ((i + 1) * w + j) * 4 + k;
					imageData.data[num] = imageData.data[num] - imageData.data[numUp -
						4] + 128;
				}
			}
		}
		return imageData
	},
	// 8.雾化
	h: function(imageData) {
		let w = imageData.width,
			h = imageData.height;

		for (let i = h; i > 0; i--) { // 行
			for (let j = w; j > 0; j--) { // 列
				let num = (i * w + j) * 4;
				if (Math.random() < 0.1) {
					imageData.data[num] = 255;
					imageData.data[num + 1] = 255;
					imageData.data[num + 2] = 255;
				}
			}
		}
		return imageData
	},
	// 9.毛玻璃
	j: function(canvasData) {
		let w = canvasData.width,
			h = canvasData.height;
		for (let i = 0; i < h; i++) {
			for (let j = 0; j < w; j++) {
				for (let k = 0; k < 3; k++) {
					// Index of the pixel in the array  
					let num = (i * w + j) * 4 + k;
					let rand = Math.floor(Math.random() * 10) % 3;
					let num2 = ((i + rand) * w + (j + rand)) * 4 + k;
					canvasData.data[num] = canvasData.data[num2]
				}
			}
		}
		return canvasData
	},
	// 10.马赛克
	k: function(imageData, size) {
		let w = imageData.width,
			h = imageData.height;
		for (let i = 1; i < h - 1; i += size) {
			for (let j = 1; j < w - 1; j += size) {
				let num = (i * w + j) * 4;
				for (let dx = 0; dx < size; dx++) {
					for (let dy = 0; dy < size; dy++) {
						let x = i + dx;
						let y = j + dy;
						let p1 = (x * w + y) * 4;
						imageData.data[p1 + 0] = imageData.data[num + 0];
						imageData.data[p1 + 1] = imageData.data[num + 1];
						imageData.data[p1 + 2] = imageData.data[num + 2];
					}
				}
			}
		}
		return canvasData
	},
	// 11.模糊
	i: function(imageData) {
		let w = imageData.width,
			h = imageData.height;
		let data1 = imageData.data;
		let data2 = imageData.data;
		for (let i = 0; i < h; i++) { // 行
			for (let j = 0; j < w; j++) { // 列
				for (let k = 0; k < 3; k++) {
					let num = (i * w + j) * 4 + k;
					let numUp = ((i - 1) * w + j) * 4 + k;
					let numDown = ((i + 1) * w + j) * 4 + k;
					data1[num] = (data2[numUp - 4] + data2[numUp] + data2[numUp + 4] +
						data2[num - 4] + data2[num] + data2[num + 4] +
						data2[numDown - 4] + data2[numDown] + data2[numDown + 4]) / 9;
				}
			}
		}
		return imageData
	}
}

